#pragma once

#include "DataInterface.h"
#include "Locks.h"
#include <windows.h>

class DataInterfaceClassFactory : public IClassFactory
{
public:
	DataInterfaceClassFactory() {
		m_refCount = 0;
	}
	virtual ~DataInterfaceClassFactory() {
		/*MessageBox(NULL,
			L"MyCarClassFactory is being distructed. Make sure you see this message, if not, you might have memory leak!",
			L"Destructor",MB_OK | MB_SETFOREGROUND);*/
	}
	// IUnknown
	STDMETHODIMP QueryInterface(REFIID riid,void** pIFace) {
		if ( riid == IID_IUnknown )
			*pIFace = (IUnknown*)this;
		else if ( riid == IID_IClassFactory )
			*pIFace = (IClassFactory*)this;
		else
		{
			*pIFace = NULL;
			return E_NOINTERFACE;
		}
		((IUnknown*)(*pIFace))->AddRef();
		return S_OK;
	}
	STDMETHODIMP_(ULONG)AddRef() {
		return 10;
	}
	STDMETHODIMP_(ULONG)Release() {
		return 20;
	}

	// IClassFactory
	STDMETHODIMP LockServer(BOOL fLock) {
		if ( fLock ) Lock();
		else UnLock();
		return S_OK;
	}
	STDMETHODIMP CreateInstance(LPUNKNOWN pUnkOuter,REFIID riid,void** ppv) {
		if ( pUnkOuter != NULL ) return CLASS_E_NOAGGREGATION;

		HRESULT hr;

		DataInterface* pDataInterfaceObj = new DataInterface();
		hr = pDataInterfaceObj->QueryInterface(riid,ppv);

		if ( FAILED(hr) ) {
			//MessageBox(0, L"Faild query interface, delete instance", L"", MB_OK);
			delete pDataInterfaceObj;
		}
		return hr;
	}

private:

	ULONG m_refCount;

};